package com.syni.mdd.yhd.common.all.dao.jpa;

import com.syni.mdd.yhd.common.all.dao.BaseJPADao;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessGroupMessage;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface BmsBusinessGroupMessageDao extends BaseJPADao<BmsBusinessGroupMessage,Integer> {
    @Query(value = "select count(bbgm.id) num from bms_business_group_message bbgm where bbgm.bms_business_id = ?1" +
            " and bbgm.type = ?2 and unix_timestamp(bbgm.new_time) >= ?3 and unix_timestamp(bbgm.new_time) <= ?4",nativeQuery = true)
    Integer selfFindBusinessSendNum(Integer businessId,Integer sendType,Integer startTime,Integer endTime);

    @Query(value = "select count(bgm.id) num,bgm.bms_business_id businessId from bms_business_group_message bgm left join bms_user_rece_group_message burgm on burgm.group_message_id = bgm.id where bms_user_id = ?1 and bgm.bms_business_id in (?2) and is_read = ?3 " +
            "group by bgm.bms_business_id",nativeQuery = true)
    List<Map<String,Object>> selfFindUserBusinessMessage(Integer userId,Set businessIds,Integer isRead);

    @Query(value = "select bgm.id,bgm.mes_type mesType,bgm.bms_business_id,bgm.content,bgm.new_time,burgm.id burgmId,bgm.activity_id activityId from bms_business_group_message bgm left join bms_user_rece_group_message burgm on burgm.group_message_id = bgm.id " +
            "where burgm.bms_user_id = ?1 and bgm.bms_business_id = ?2 and burgm.status = 1",nativeQuery = true)
    List<Map<String,Object>> selfFindUserBusinessMessage(Integer userId,Integer businessIds,Pageable pageable);

    List<BmsBusinessGroupMessage> findByBmsBusinessIdAndType(Integer businessId,Integer type,Pageable pageable);

    BmsBusinessGroupMessage findByIdAndIsDelete(Integer groupId,Integer isDelete);
}
